<?php


namespace App\Aspect;


use App\Annotation\TestAnnotation;
use App\Controller\TestController;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Utils\Codec\Json;

/**
 * @Aspect()
 */
class TestAspect extends AbstractAspect
{
    public $classes = [
        TestController::class
    ];

    public $annotations = [
        TestAnnotation::class
    ];

    public function process(ProceedingJoinPoint $proceedingJoinPoint)
    {
        var_dump('before');
        $response = $proceedingJoinPoint->process();
        var_dump('after');

        $body = $response->getBody()->getContents();
        $body = Json::decode($body);
        $body['Aspect'] = __CLASS__;
        $body = Json::encode($body);
        return $response->withBody(new SwooleStream($body));
    }

}